package fr.utbm.aiechat.packets;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import fr.utbm.aiechat.SharedFilesManager;
import fr.utbm.aiechat.structures.SharedFile;

public class UserSharedResponsePacket
{
	private ByteArrayOutputStream stream;

	public UserSharedResponsePacket()
	{
		stream = new ByteArrayOutputStream();
		try
		{
			ObjectOutputStream dataObj = new ObjectOutputStream(stream);
			
			dataObj.writeInt(Packets.USERSHARED_RESPONSE);
			
			ArrayList<SharedFile> myFiles = SharedFilesManager.getInstance().getMyFiles();
			
			Iterator<SharedFile> itr = myFiles.iterator();
			
			dataObj.writeInt(myFiles.size());
			
			while (itr.hasNext())
			{
				SharedFile element = itr.next();
				
				dataObj.writeObject(element);
			}
			
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public byte[] getPacket()
	{
		return stream.toByteArray();
	}
}
